package com.sunday.boot3.user.center.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sunday.common.core.asserts.BusinessAssert;
import com.sunday.common.core.converter.MapperConvert;
import com.sunday.common.core.enums.ErrorCodeEnum;
import com.sunday.common.core.remoting.base.page.PageResultDTO;
import com.sunday.common.mybatis.plus.query.PageWrapper;
import com.sunday.common.leaf.core.IdWorker;
import com.sunday.boot3.user.center.pojo.dto.role.permissions.SaveRolePermissionsReqDTO;
import com.sunday.boot3.user.center.pojo.dto.role.permissions.UpdateRolePermissionsReqDTO;
import com.sunday.boot3.user.center.pojo.dto.role.permissions.PageRolePermissionsResDTO;
import com.sunday.boot3.user.center.pojo.dto.role.permissions.PageRolePermissionsReqDTO;
import com.sunday.boot3.user.center.pojo.dto.role.permissions.RemoveRolePermissionsReqDTO;
import com.sunday.boot3.user.center.repository.dao.RolePermissionsDao;
import com.sunday.boot3.user.center.repository.entity.RolePermissions;
import com.sunday.boot3.user.center.service.RolePermissionsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @Description: 角色权限关系表 服务实现类
 * @author: zsy
 * @Create: 2024/09/22 16:24:38
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class RolePermissionsServiceImpl implements RolePermissionsService {

    private final RolePermissionsDao rolePermissionsDao;

    /**
     * @Description: 新增 角色权限关系表 数据
     * @Param: [saveRolePermissionsReqDTO]
     * @Return: void
     * @Author: zsy
     * @Time: 2024/09/22 16:24:38
     */
    @Override
    public void save(SaveRolePermissionsReqDTO saveRolePermissionsReqDTO) {
        RolePermissions rolePermissions = MapperConvert.map(saveRolePermissionsReqDTO, RolePermissions.class);
        rolePermissions.setId(IdWorker.getId());
        rolePermissions.setCreateTime(LocalDateTime.now());
        BusinessAssert.isTrue(rolePermissionsDao.save(rolePermissions), ErrorCodeEnum.A0_440);
    }

    /**
     * @Description: 删除 角色权限关系表 数据
     * @Param: [removeRolePermissionsReqDTO]
     * @Return: void
     * @Author: zsy
     * @Time: 2024/09/22 16:24:38
     */
    @Override
    public void remove(RemoveRolePermissionsReqDTO removeRolePermissionsReqDTO) {
        BusinessAssert.isTrue(rolePermissionsDao.removeByIds(removeRolePermissionsReqDTO.getIds()), ErrorCodeEnum.A0_440);
    }

    /**
     * @Description: 修改 角色权限关系表 数据
     * @Param: [updateRolePermissionsReqDTO]
     * @Return: void
     * @Author: zsy
     * @Time: 2024/09/22 16:24:38
     */
    @Override
    public void update(UpdateRolePermissionsReqDTO updateRolePermissionsReqDTO) {
        LambdaUpdateWrapper<RolePermissions> rolePermissionsUW = Wrappers.lambdaUpdate();
        rolePermissionsUW.eq(RolePermissions::getId, updateRolePermissionsReqDTO.getId());
        rolePermissionsUW.set(RolePermissions::getRoleId, updateRolePermissionsReqDTO.getRoleId())
                .set(RolePermissions::getPermissionsId, updateRolePermissionsReqDTO.getPermissionsId());
        BusinessAssert.isTrue(rolePermissionsDao.update(rolePermissionsUW), ErrorCodeEnum.A0_440);
    }

    /**
     * @Description: 获取 角色权限关系表 分页列表数据
     * @Param: [pageRolePermissionsReqDTO]
     * @Return: com.sunday.common.core.remoting.base.page.PageResultDTO<com.sunday.boot3.user.center.pojo.dto.role.permissions.PageRolePermissionsResDTO>
     * @Author: zsy
     * @Time: 2024/09/22 16:24:38
     */
    @Override
    public PageResultDTO<PageRolePermissionsResDTO> page(PageRolePermissionsReqDTO pageRolePermissionsReqDTO) {
        LambdaQueryWrapper<RolePermissions> rolePermissionsQW = Wrappers.lambdaQuery();
        rolePermissionsQW.eq(ObjectUtils.isNotEmpty(pageRolePermissionsReqDTO.getId()), RolePermissions::getId, pageRolePermissionsReqDTO.getId())
                .eq(ObjectUtils.isNotEmpty(pageRolePermissionsReqDTO.getRoleId()), RolePermissions::getRoleId, pageRolePermissionsReqDTO.getRoleId())
                .eq(ObjectUtils.isNotEmpty(pageRolePermissionsReqDTO.getPermissionsId()), RolePermissions::getPermissionsId, pageRolePermissionsReqDTO.getPermissionsId())
                .eq(ObjectUtils.isNotEmpty(pageRolePermissionsReqDTO.getCreateTime()), RolePermissions::getCreateTime, pageRolePermissionsReqDTO.getCreateTime());
        return PageWrapper.result(rolePermissionsDao.page(PageWrapper.map(pageRolePermissionsReqDTO, RolePermissions::getId), rolePermissionsQW), data -> MapperConvert.map(data, PageRolePermissionsResDTO.class));
    }

}
